import goodsCapacityApi from "@/api/store/goodsCapacity";
import putApi from "@/api/store/put";
import dictApi from "@/api/system/dict";
import houseApi from "@/api/store/house";

export default {
  name: "goodscapacity",
  data() {
    return {
      listQuery: {
        current: 1,
        size: 20,
        criteria: {
          inBoundType: "0",
          inBoundResponsible: "",
          orderNum: "",
        },
      },
      list: null,
      total: 0,
      listLoading: true,
      selRow: {},
      formVisible: false,
      goodsDetailList: [],
      baseUrl: process.env.VUE_APP_BASE_API,
      goodsCapacity: {
        goodsName: "",
        goodsType: "",
        price: 0,
        priceUnit: "",
        specification: [],
        capacity: 0,
      },
      addGoodsCapacityVisible: false,
      houseList: [],
      inBountVisible:false,
      InBoundList:{
        inBoundPerson:'',
        warehouseId:'',
        inBoundType:'',
        inBoundTypeId:'',
        inBoundResponsible:'',
        orderNum:'',
        inBoundPerson:''
      },
      rules:{
        inBoundPerson: [
          { required: true, message: "请输入入库人", trigger: "blur" },
        ],
        warehouseId: [
          { required: true, message: "请选择仓库", trigger: "change" },
        ]
      }
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      this.fetchData();
      this.getTypeDict();
    },
    fetchData() {
      this.listLoading = true;
      putApi
        .getList(this.listQuery)
        .then((response) => {
          this.list = response.data.records;
          this.listLoading = false;
          this.total = response.data.total;
        })
        .catch(() => {});
    },
    generNum() {
      return Math.floor(Math.random() * 20);
    },
    getTypeDict() {
      // 从后台获取字典列表
      let dictName = "商品类型";
      dictApi.getDicts(dictName).then((response) => {
        this.goodsTypeList = response.data[0].children ?? [];
      });
    },
    getTypeValueDict(num) {
      const arr = this.goodsTypeList;
      if (arr) {
        for (let i = 0; i < arr.length; i++) {
          if (arr[i].num == num) {
            return arr[i].name;
          }
        }
      }
    },
    search() {
      this.listQuery.current = 1;
      this.fetchData();
    },
    reset() {
      this.listQuery.criteria.inBoundType = "0";
      this.listQuery.criteria.inBoundResponsible = "";
      this.listQuery.criteria.orderNum = "";
      this.listQuery.current = 1;
      this.fetchData();
    },
    handleFilter() {
      this.listQuery.current = 1;
      this.fetchData();
    },
    handleClose() {},
    fetchNext() {
      this.listQuery.current = this.listQuery.current + 1;
      this.fetchData();
    },
    fetchPrev() {
      this.listQuery.current = this.listQuery.current - 1;
      this.fetchData();
    },
    fetchPage(page) {
      this.listQuery.current = page;
      this.fetchData();
    },
    changeSize(limit) {
      this.listQuery.size = limit;
      this.fetchData();
    },
    handleCurrentChange(currentRow, oldCurrentRow) {
      this.selRow = currentRow;
    },
    showDetail(id, type) {
      if (!id) {
      }
      //查询详情
        putApi.getGoodsDetail(id, type).then((response) => {
          this.goodsDetailList = response.data;
          this.formVisible = true;
        });
      
    },
    addGoodsCapacity(row) {
      this.selRow = row;
      this.goodsCapacity.goodsName = row.name;
      this.goodsCapacity.goodsType = row.type;
      this.goodsCapacity.specification = row.specification;
      this.goodsCapacity.price = row.price;
      this.goodsCapacity.priceUnit = row.priceUnit;
      this.goodsCapacity.capacity = 0;
      this.addGoodsCapacityVisible = true;
    },
    SaveCapacity() {
      let self = this;
      
       
          goodsCapacityApi.save(this.goodsCapacity).then((response) => {
            this.$message({
              message: "提交成功",
              type: "success",
            });
            self.selRow.existGoodsCapacity = true;
            self.addGoodsCapacityVisible = false;
          });
       
    },
    inBount(row){
  
      if(row.inBoundType == 0){
      this.InBoundList.inBoundResponsible = row.inBoundResponsible
      this.InBoundList.inBoundType = row.inBoundType
      this.InBoundList.inBoundTypeId = row.inBoundTypeId
      this.InBoundList.orderNum = row.orderNum
      //先计算容量和入库商品是否存在入库规格
      putApi.calculateCapacity(row.inBoundTypeId,row.inBoundType).then((response) => {
        let total = response.data.totalCapacity
        let flag = response.data.flag
        if(!flag){
          this.$message( {
            message: "该入库单尚有商品没有入库规格，请点击商品明细添加",
            type: "warning",
          });
        }else{
          //获取可入库仓库
          houseApi.list(total).then((response) => {
            this.houseList = response.data;
            this.inBountVisible = true;
          });
        }
        
      });
    }else if(row.inBoundType == 1){
      //退货入库，直接原仓库入库
      putApi.backInBoundList(row.inBoundTypeId).then((response) => {
        this.$message({
          message: "退货入库成功",
          type: "success",
        });
        this.fetchData();
      });
    }
    },
    saveInBoundList(){
      let self = this;
                    this.$refs["InBoundList"].validate((valid) => {
                      if (valid) {
                        putApi.saveInBoundList(this.InBoundList).then((response) => {
                          this.$message({
                            message: "入库成功",
                            type: "success",
                          });
                          self.inBountVisible = false;
                          self.fetchData();
                        });
                      } else {
                        return false;
                      }
                    });
        
    }
  },
};
